<template>
    <div class="body">
        <!--    header-->
        <div class="header"></div>
        <!--    content-->
        <div class="content">
            <div class="form">
                <h2>欢迎登陆</h2>
                <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="" class="login_form">
                    <!--用户名-->
                    <el-form-item label="" prop="username">
                        <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid">
                        </el-input>
                    </el-form-item>
                    <!--密码-->
                    <el-form-item label="" prop="password">
                        <el-input  v-model="loginForm.password" prefix-icon="el-icon-lock" type="password">
                        </el-input>
                    </el-form-item>
                    <!--登陆、重置按钮-->
                    <el-form-item class="btn">
                        <button class="btn_login" @click="login">登陆</button>
                    </el-form-item>
                    <a id="text" @click="toReg"><span>没有账号？</span>免费注册</a>
                </el-form>
            </div>
            <img src="../../assets/imgs/bg.jpg" alt="">
        </div>
        <!--    footer-->
        <div class="footer"></div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单参数的数据绑定；
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登陆名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  methods: {
    //  toReg注册跳转
    toReg () {
      this.$router.push('/shop_register')
    },
    //  登陆功能的实现
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        console.log('登录操作')
        await this.$router.push({
          path: '/shop'
        })
        const { data: res } = await this.$http.post('/v2/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('登陆失败！')
        }
        console.log(res.data)
        this.$message.success('登陆成功！')
        const token = res.data.token
        window.sessionStorage.setItem('token', res.data.token)
        console.log(token)
        this.$store.commit('setStorage', token)
        await this.$router.push({
          path: '/shop'
        })
        // console.log(this.$store.state.isLogin)
      })
    }
  }
}
</script>
<style lang="less"  >
    .body{
        position: relative;
        height: 100%;
        width: 100%;
        overflow: hidden;
        /*background-color:red;*/
    }
    .header{
        width: 100%;
        height: 12%;
        /*background-color: aqua;*/
        float: left;
    }
    .content {
        width: 100%;
        height: 68%;
        float:left;
        position: relative;
        overflow: hidden;
        > img {
            width: 100%;
            height: 100%;
            z-index: -1;
            position: absolute;
            float: left;
        }
        >.form{
            width: 360px;
            height: 360px;
            background-color: #ffffff;
            z-index: 2;
            position: absolute;
            float: left;
            right: 18%;
            top: 10%;
            text-align: center;
            >.login_form {
                position: absolute;
                bottom: 0;
                top: 28%;
                width: 90%;
                padding: 0 20px;
                box-sizing: border-box;
                left: 13px;
            }
        }
    }
    .footer{
        width: 100%;
        height: 20%;
        /*background-color: white;*/
        float: left;
    }
    /*.el-input{*/
    /*    clear: both;*/
    /*    border: 1px solid rgb(255,0,54) !important;*/
    /*}*/
    .btn {
        text-align: center;
        .btn_login {
            width: 100%;
            height: 40px;
            font-size: 16px;
            line-height: 36px;
            color: #ffffff;
            border: none;
            background-color: rgb(255, 0, 54);
        }
    }
    #text{
        font-size: 12px;
        right: 5px;
        position: absolute;
        float: right;
        top: 69%;
        cursor: pointer;
      >span{
        color: gainsboro;
        cursor: text;
      }
    }
    #text:hover{
        color:rgb(255,0,54);
    }
</style>
